﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.IO;
using System.Collections.Generic;

public partial class Manage_Cms_SiteItem : System.Web.UI.Page
{
    protected KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
    protected KeRui.Model.CmsSite modelCmsSite = new KeRui.Model.CmsSite();
    public string Act = ManageHelper.PageAct();
    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    protected DataSet dsTemplate = new DataSet();
    protected int serverID = PageRequest.GetInt("serID", -1);
    private int f = PageRequest.GetInt("f", -1);
    protected KeRui.Model.CmsServer modelCmsSever = new KeRui.Model.CmsServer();
    protected KeRui.BLL.CmsTemplate bllCmsTemplate = new KeRui.BLL.CmsTemplate();//SELECT 列表

    protected void Page_Load(object sender, EventArgs e)
    {
        serverID = PageRequest.GetInt("serID", -1);
        ManageHelper.CheckAdminLogin();//登陆判断
        //权限
        ManageHelper.CheckAdminPower("system_servermanage");
        modelCmsSite.ServerID = serverID;
        KeRui.BLL.CmsServer bllCmsServer = new KeRui.BLL.CmsServer();
        //modelCmsSever = bllCmsServer.GetModel(PageRequest.GetServerIP());
        if (Act == "showsiteurl")
        {
            showsiteurl();
        }
        else
        {
            if (serverID > 0 && Act == "")
            {
                modelCmsSever = bllCmsServer.GetModel(serverID);
                if (f == -1 && modelCmsSever != null)
                {
                    Response.Redirect(modelCmsSever.ServerUrl + "/manage/cms/siteitem.aspx?f=1&serID=" + serverID);
                }
            }
            else if (Act == "")
            {
                modelCmsSever = bllCmsServer.GetModel(PageRequest.GetServerIP());
                if (modelCmsSever != null)
                {
                    Response.Redirect(modelCmsSever.ServerUrl + "/manage/cms/siteitem.aspx?f=1&serID=" + modelCmsSever.ServerID);
                }
                else
                {
                    modelCmsSever = new KeRui.Model.CmsServer();
                }
            }
            else
            {

                switch (Act)
                {
                    case "addsave":
                        CmsSiteAdd();
                        break;
                    case "edit":
                        Edit();
                        break;
                    case "editsave":
                        EditSave();
                        break;
                    case "del":
                        Del();
                        break;
                    case "server":
                        ServerDomain();
                        break;
                    case "showsiteurl":
                        showsiteurl();
                        break;
                    default:
                        break;
                }//<!--<%=bllCmsSite.GetMaxOrderID() %>-->
            }
        }
    }


    #region 添加
    private void CmsSiteAdd()
    {
        ManageHelper.CheckAdminPower("system_siteadd");//权限
        //KeRui.BLL.CmsSite
        bllCmsSite = new KeRui.BLL.CmsSite();
        //KeRui.Model.CmsSite modelCmsSite = new KeRui.Model.CmsSite();
        modelCmsSite.SiteName = PageRequest.GetString("SiteName");
        if (modelCmsSite.SiteName == "")
        {
            nsb.AddErr("请输入站点名称。");
        }
        modelCmsSite.SiteAlias = PageRequest.GetString("SiteAlias");
        //if (modelCmsSite.SiteAlias == "")
        //{
        //    nsb.AddErr("请输入站点别名。");
        //}
        modelCmsSite.SiteKey = PageRequest.GetString("SiteKey");
        //if (modelCmsSite.SiteKey == "")
        //{
        //    nsb.AddErr("请输入站点标识符。");
        //}
        modelCmsSite.SiteDir = PageRequest.GetString("SiteDir");
        if (modelCmsSite.SiteDir == "")
        {
            nsb.AddErr("请输入站点路径。");
        }
        else
        {
            modelCmsSite.SiteDir = modelCmsSite.SiteDir + @"\" + modelCmsSite.SiteKey;
        }
        modelCmsSite.TemplateID = PageRequest.GetInt("TemplateID", -1);
        if (modelCmsSite.TemplateID == -1)
        {
            nsb.AddErr("请选择模板。");
        }
        modelCmsSite.ServerID = PageRequest.GetInt("ServerID", -1);
        if (modelCmsSite.ServerID == -1)
        {
            nsb.AddErr("请选择服务器。");
        }
        modelCmsSite.OrderID = PageRequest.GetInt("OrderID", -1);
        if (modelCmsSite.OrderID == -1)
        {
            nsb.AddErr("排序应该为一个整数。");
        }
        modelCmsSite.IsUsing = PageRequest.GetInt("IsUsing", 0);
        nsb.ChkErr();
        try
        {
            //FileObj.FolderCreate(modelCmsSite.SiteDir, modelCmsSite.SiteKey);//站点
            string strV = modelCmsSite.SiteDir.Substring(0, modelCmsSite.SiteDir.IndexOf(@"\") + 1);
            string strT = modelCmsSite.SiteDir.Substring(modelCmsSite.SiteDir.IndexOf(@"\") + 1);
            CreatePathWithPath(strV, strT);
        }
        catch
        {
            nsb.AddErr("文件路径有误！"); nsb.ChkErr();
        }
        int RetFlag = bllCmsSite.Add(modelCmsSite);
        if (RetFlag == -1)
        {
            nsb.AddErr("对不起！已存在相同站点标识符。");
            nsb.ChkErr();
        }
        else
        {
            //KeRui.BLL.Common.CreateSite(modelCmsSite.SiteDir, 1);
            ManageHelper.AddLog("system_Selleradd", "添加了“" + modelCmsSite.SiteName + "”站点。");
            //FileObj.FolderCreate(@"d:\iPortal\KeRui.Source\Template", modelCmsSite.SiteKey);//模板
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"sitelist.aspx?name=" + Utils.UrlEncode(modelCmsSite.SiteName,"UTF-8") + "&sucmsg=添加成功。\";</script>");
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ActFlag = "editsave";
        ActName = "修改";
        int SiteID = PageRequest.GetInt("ID", -1);
        if (SiteID == -1)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        modelCmsSite = bllCmsSite.GetModel(SiteID);
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_siteedit");//权限
        modelCmsSite.SiteID = PageRequest.GetInt("SiteID", -1);
        if (modelCmsSite.SiteID == -1 || modelCmsSite.SiteID == 0)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        modelCmsSite.SiteName = PageRequest.GetString("SiteName");
        if (modelCmsSite.SiteName == "")
        {
            nsb.AddErr("请输入站点名称。");
        }
        modelCmsSite.SiteAlias = PageRequest.GetString("SiteAlias");
        //if (modelCmsSite.SiteAlias == "")
        //{
        //    nsb.AddErr("请输入站点别名。");
        //}
        modelCmsSite.SiteKey = PageRequest.GetString("SiteKey");
        //if (modelCmsSite.SiteKey == "")
        //{
        //    nsb.AddErr("请输入站点标识符。");
        //}
        modelCmsSite.TemplateID = PageRequest.GetInt("TemplateID", -1);
        if (modelCmsSite.TemplateID == -1)
        {
            nsb.AddErr("请选择模板。");
        }
        modelCmsSite.ServerID = PageRequest.GetInt("ServerID", -1);
        if (modelCmsSite.ServerID == -1)
        {
            nsb.AddErr("请选择服务器。");
        }
        modelCmsSite.OrderID = PageRequest.GetInt("OrderID", -1);
        if (modelCmsSite.OrderID == -1)
        {
            nsb.AddErr("排序应该为一个整数。");
        }
        modelCmsSite.IsUsing = PageRequest.GetInt("IsUsing", 0);
        nsb.ChkErr();
        int RetFlag = bllCmsSite.Update(modelCmsSite);
        if (RetFlag == -1)
        {
            nsb.AddErr("对不起！已存在相同站点标识符。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_Selleradd", "修改了“" + modelCmsSite.SiteName + "”站点。");
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"sitelist.aspx?sucmsg=修改成功。\";</script>");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("system_sitedel");//权限
        int SiteID = PageRequest.GetInt("ID", -1);
        if (SiteID == -1)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        modelCmsSite = bllCmsSite.GetModel(SiteID);
        if (modelCmsSite == null)
        {
            nsb.AddErr("该站点信息不存在。");
            nsb.ChkErr();
        }

        KeRui.BLL.CmsContent bllCmsContent = new KeRui.BLL.CmsContent();
        KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
        List<KeRui.Model.CmsNode> listNodes = bllCmsNode.GetList(SiteID);

        foreach (KeRui.Model.CmsNode model in listNodes)
        {
            string nodeDir = bllCmsNode.GetModel(model.NodeID).NodeDir;
            string file = nodeDir;

            DirectoryInfo dir = new DirectoryInfo(nodeDir);

            if (dir.Exists)
            {
                try
                {
                    dir.Delete(true);//删除文件夹
                }
                catch (Exception exx)
                {
                    nsb.AddErr(exx.Message);
                    nsb.ChkErr();
                }
                
            }
        }

        bllCmsSite.Delete(SiteID);
        ManageHelper.AddLog("system_sitedel", "删除了“" + modelCmsSite.SiteName + "”站点。");
        Response.Redirect("SiteList.aspx?sucmsg=恭喜。删除成功。");
    }
    #endregion

    #region 获取server中的Domain
    private void ServerDomain()
    {
        int ServerID = PageRequest.GetInt("ID", -1);
        KeRui.BLL.CmsServer bllCmsServer = new KeRui.BLL.CmsServer();
        KeRui.Model.CmsServer modelCmsServer = bllCmsServer.GetModel(ServerID);
        if (modelCmsServer == null)
        {
            Response.Write("ERROR!");
        }
        Response.Write(modelCmsServer.Domain);
        Response.End();
    }
    #endregion


    #region 根据路径循环创建文件夹
    private void CreatePathWithPath(string strV, string strT)
    {
        if (strT.IndexOf(@"\") >= 0)
        {
            string[] strTs = strT.Split(@"\".ToCharArray());
            for (int i = 0; i < strTs.Length; i++)
            {
                DirectoryInfo Fsofolder = new DirectoryInfo(strV + strT);
                if (!Fsofolder.Exists)
                {
                    Fsofolder.Create();
                }
            }
        }
    }
    #endregion


    #region  显示网站地址
    private void showsiteurl()
    {
        KeRui.BLL.SysLicense bllsyslicense = new KeRui.BLL.SysLicense();
        List<KeRui.Model.SysLicense> list = new List<KeRui.Model.SysLicense>();
        list = bllsyslicense.GetServerDomain(PageRequest.GetServerIP());
        string strUrl = "";
        string sitekey = PageRequest.GetString("sitekey");
        int i = 0;
        foreach (KeRui.Model.SysLicense model in list)
        {
            i++;
            string ifcheck = "";
            if (i == 1)
                ifcheck = "checked";
            strUrl += "<input type=\"radio\" name=\"SiteUrl\" " + ifcheck + " value=\"http://" + sitekey + "." + model.Domain + "\"> http://" + sitekey + "." + model.Domain + "<br/>";
        }
        Response.Write(strUrl); Response.End();
    }
    #endregion
}
